define(['jquery'], function () {
    class Header {
        constructor() {
            this.loadHtml().then(() => {
                this.search()
                this.calcCartCount()
            })

        }
        loadHtml() {
            //加载头部HTML
            return new Promise(resolve => {
                $('header').load('/html/modules/header.html', resolve)
            })

        }
        search() {
            //头部搜索框
            $('#ipt').on('keyup', function () {
                const wd = $(this).val()
                $.getJSON('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?cb=?', {
                    wd
                }, resp => {

                    resp.s.forEach(element => {
                        console.log(element)
                        $('#ul').append($("<li></li>").html(element))

                    });
                    $('#ul').css("display", "block")
                })

            })
            $('#ipt').on('blur', function () {
                $('#ul').css("display", "none")
            })
        }
        calcCartCount() {
            let totalCount = 0
            let cart = localStorage.getItem('cart')
            if (cart) {
                cart = JSON.parse(cart)
                totalCount = cart.reduce((count, shop) => {
                    count += shop.count
                    return count
                }, 0)
            }
            $('#cartCount,#cart_Count').html(totalCount)


        }

    }
    return new Header()
});